﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNS.Models
{
    public class PhongBanModels
    {
        
        public static List<PhongBan> GetDSPhongBan(int idparent)
        {

            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
           
            var query = from dt in context.DSPhongBan
                        where dt.IDParent == idparent
                        orderby dt.PhanLoai, dt.STT
                        select dt;
            return query.ToList<PhongBan>();
        }
        public static List<PhongBan> GetDSPhongBanNotDepartment(int idparent)
        {

            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var query = from dt in context.DSPhongBan
                        where dt.IDParent == idparent && dt.PhanLoai == 1
                        orderby dt.PhanLoai, dt.STT
                        select dt;
            return query.ToList<PhongBan>();
        }
        //danh sach phongban
        public static PhongBan GetChiTietPhongBan(int id)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from s in context.DSPhongBan
                         where s.ID == id
                         select s).FirstOrDefault();
            return query;

        }
        public static String GetNameDepartment(int id)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from s in context.DSPhongBan
                         where s.ID == id
                         select s).FirstOrDefault();
            return query.TenPhongBan;
        }

        public static List<int> GetAllIDToRootInDepartment(int id)
        {
            List<int> lstID = new List<int>();
            PhongBan parent = GetChiTietPhongBan(id);
            lstID.Add(parent.ID);


            List<PhongBan> root = GetDSPhongBanNotDepartment(id);
            if (root.Count > 0)
            {
                //Cap 1 la Don vi       
                foreach (var r in root)
                {
                    //listRootDept.Add(new SelectListItem { Value = r.ID.ToString(), Text = r.TenPhongBan });
                    lstID.Add(r.ID);
                    List<PhongBan> root1 = GetDSPhongBanNotDepartment(r.ID);
                    if (root1.Count > 0)
                    {
                        foreach (var r1 in root1)
                        {
                            //listRootDept.Add(new SelectListItem { Value = r1.ID.ToString(), Text = "- " + r1.TenPhongBan });
                            lstID.Add(r1.ID);
                            List<PhongBan> root2 = GetDSPhongBanNotDepartment(r1.ID);
                            if (root2.Count > 0)
                            {
                                foreach (var r2 in root2)
                                {
                                    //listRootDept.Add(new SelectListItem { Value = r2.ID.ToString(), Text = "- - " + r2.TenPhongBan });
                                    lstID.Add(r2.ID);
                                    List<PhongBan> root3 = GetDSPhongBanNotDepartment(r2.ID);
                                    if (root3.Count > 0)
                                    {
                                        foreach (var r3 in root3)
                                        {
                                            //listRootDept.Add(new SelectListItem { Value = r3.ID.ToString(), Text = "- - - " + r3.TenPhongBan });
                                            lstID.Add(r3.ID);
                                            List<PhongBan> root4 = GetDSPhongBanNotDepartment(r3.ID);
                                            if (root4.Count > 0)
                                            {
                                                foreach (var r4 in root4)
                                                {
                                                    //listRootDept.Add(new SelectListItem { Value = r4.ID.ToString(), Text = "- - - - " + r4.TenPhongBan });
                                                    lstID.Add(r4.ID);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return lstID;
        }


        public static List<int> GetAllIDToRootInDepartmentInclude(int id)
        {
            List<int> lstID = new List<int>();
            PhongBan parent = GetChiTietPhongBan(id);
            lstID.Add(parent.ID);


            List<PhongBan> root = GetDSPhongBan(id);
            if (root.Count > 0)
            {
                //Cap 1 la Don vi       
                foreach (var r in root)
                {
                    //listRootDept.Add(new SelectListItem { Value = r.ID.ToString(), Text = r.TenPhongBan });
                    lstID.Add(r.ID);
                    List<PhongBan> root1 = GetDSPhongBan(r.ID);
                    if (root1.Count > 0)
                    {
                        foreach (var r1 in root1)
                        {
                            //listRootDept.Add(new SelectListItem { Value = r1.ID.ToString(), Text = "- " + r1.TenPhongBan });
                            lstID.Add(r1.ID);
                            List<PhongBan> root2 = GetDSPhongBan(r1.ID);
                            if (root2.Count > 0)
                            {
                                foreach (var r2 in root2)
                                {
                                    //listRootDept.Add(new SelectListItem { Value = r2.ID.ToString(), Text = "- - " + r2.TenPhongBan });
                                    lstID.Add(r2.ID);
                                    List<PhongBan> root3 = GetDSPhongBan(r2.ID);
                                    if (root3.Count > 0)
                                    {
                                        foreach (var r3 in root3)
                                        {
                                            //listRootDept.Add(new SelectListItem { Value = r3.ID.ToString(), Text = "- - - " + r3.TenPhongBan });
                                            lstID.Add(r3.ID);
                                            List<PhongBan> root4 = GetDSPhongBan(r3.ID);
                                            if (root4.Count > 0)
                                            {
                                                foreach (var r4 in root4)
                                                {
                                                    //listRootDept.Add(new SelectListItem { Value = r4.ID.ToString(), Text = "- - - - " + r4.TenPhongBan });
                                                    lstID.Add(r4.ID);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return lstID;
        }
        //them vao bang phongban
        public static void Add(PhongBan pb)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            context.DSPhongBan.AddObject(pb);
            context.SaveChanges();
        }
        //Xoa phongban
        public static void Delete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from h in context.DSPhongBan
                         where h.ID == id
                         select h).FirstOrDefault();

            context.DSPhongBan.DeleteObject(query);
            context.SaveChanges();

        }

        //sua phongban
        public static void Edit(PhongBan pb)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from h in context.DSPhongBan
                        where h.ID == pb.ID
                        select h;
            PhongBan kq = query.First<PhongBan>();
            kq = pb;
            context.DSPhongBan.ApplyCurrentValues(kq);
            context.SaveChanges();

        }
    }
}